package com.fzu.mmdbproxyframework.configurationmanager;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.fzu.mmdbproxyframework.context.ClusterContext;
import com.fzu.mmdbproxyframework.model.DBInterf;
import com.fzu.mmdbproxyframework.model.DataSource;
import com.fzu.mmdbproxyframework.model.MMDataNodeCluster;
import com.fzu.mmdbproxyframework.model.MysqlDB;


public class SourceDBConfigRead {
    
	public MMDataNodeCluster cluster;
	
    /**
	 * 
	 */
	private SourceDBConfigRead() {
		super();
	}


	/**
	 * @param cluster
	 */
	public SourceDBConfigRead(MMDataNodeCluster cluster) {
		super();
		this.cluster = cluster;
	}


	/**
     * DBParam 参数说明：type=mysql 底层数据库的SourceDB的数据类型 目前就是默认MysqL
     * 				name   底层数据库的的名字DBNodeName
     * 				mode   底层数据库的的模式  mode="Source" 目前就是这个参数   （预留后面拓展用）
     * dataSourceParamList的参数说明：
     * 				dataSource_name  数据源的名字 
     * 				dataSource_type  数据源的类型为master
     * 				dataSource_dbType  底层数据库的SourceDB的数据类型 为MysqL
     * 				dataSource_dbMode   预留  现在固定用update、
     * 				dataSource_url 数据源的url 例子jdbc:mysql://127.0.0.1:3306/examdb
     * 				dataSource_username  数据源的用户名
     * 				dataSource_password 数据源的密码
     * */

	public  List<DBInterf> buildSourceDB(Map<String,String> SourceDBParam, List<Map<String,String>> dataSourceParamList){
		DBInterf DBNode = null;
		if(SourceDBParam.get("type").equalsIgnoreCase("mysql")){
			DBNode = new MysqlDB(cluster) ;//创建一个数据节点 datanode
		}else if(SourceDBParam.get("type").equalsIgnoreCase("Oracle")){
			System.out.println("Oracle=-数据ku类型"); 
		}
		DBNode.setDBNodeName(SourceDBParam.get("name"));
		DBNode.setDBNodeType(SourceDBParam.get("type"));
		DBNode.setMode(SourceDBParam.get("mode"));
		for(Map<String,String> dataSourcemap:dataSourceParamList){
			DataSource dataSource = new DataSource(cluster);
			dataSource.setDataSourcename(dataSourcemap.get("dataSource_name"));
			dataSource.setType(dataSourcemap.get("dataSource_type"));
			dataSource.setDBType(dataSourcemap.get("dataSource_dbType"));
			dataSource.setDBmode(dataSourcemap.get("dataSource_dbMode"));
			dataSource.setUrl(dataSourcemap.get("dataSource_url"));
			dataSource.setUsername(dataSourcemap.get("dataSource_username")); 
			dataSource.setPassword(dataSourcemap.get("dataSource_password"));
			dataSource.setShutdown(false);
			DBNode.getDataSourceList().add(dataSource);//添加多个dataSourceNodes信息
		}
		cluster.getClusterContext().getSourceDBNodeList().add(DBNode);
		return cluster.getClusterContext().getSourceDBNodeList();
	}
	
    
    public  List<DBInterf> buildDataNodeList() {
    	 SAXReader reader = new SAXReader(); 
    	 InputStream in = SourceDBConfigRead.class.getClassLoader().getResourceAsStream("SourceDB.xml");  
         Document doc;
		try {
			doc = reader.read(in);
             Element host = doc.getRootElement(); 
             if (host == null) return null;
             System.err.println(host.attributeValue("name"));
             // all college node List<Element> childNodes = root.elements();  
             List<Element> Nodes = host.elements();
             System.out.println("datanodeNodes.getLength()="+Nodes.size());
             for(int i = 0; i < Nodes.size(); i++) {//添加多个datanode节点
//            	 DataNode dataNode = new DataNode();//创建一个数据节点 datanode
            	 MysqlDB mysqlDBNode = new MysqlDB(cluster) ;//创建一个数据节点 datanode
            	 Element node = Nodes.get(i);
                 if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
                     System.err.println("\t" + node.attributeValue("name"));
                     System.err.println("\t" + node.attributeValue("type"));
                     mysqlDBNode.setDBNodeName(node.attributeValue("name"));
                     mysqlDBNode.setDBNodeType(node.attributeValue("type"));
                     mysqlDBNode.setMode(node.attributeValue("mode"));
                     // all class node
                     List<Element> datasourceNod = node.elements();
                     List<DataSource> dataSourceList = new LinkedList<DataSource>();
                     for (int j = 0; j < datasourceNod.size(); j++) {//添加多个dataSourceNodes信息
                    	 Element datasource = datasourceNod.get(j);
                         DataSource dataSource = new DataSource(cluster);  
                         if (datasource != null && datasource.getNodeType() == Node.ELEMENT_NODE) {
                             System.err.println("\t\t" + datasource.attributeValue("name"));
                             System.err.println(" " + datasource.attributeValue("type"));
                             dataSource.setDataSourcename(datasource.attributeValue("name"));
                             dataSource.setType(datasource.attributeValue("type"));
                             dataSource.setDBType(datasource.attributeValue("DBType"));
                             dataSource.setDBmode(datasource.attributeValue("DBmode"));
                             // all student node
                             List<Element> propertyNodes = datasource.elements();
                             for (int k = 0; k < propertyNodes.size(); k++) {//添加dataSourceNod的多个属性
                            	 Element property = propertyNodes.get(k);
                                 if (property != null && property.getNodeType() == Node.ELEMENT_NODE) {
                                	String str = property.attributeValue("name");
                                	String strvalue = property.getText();
                                     System.err.print("\t\t\t" + str);
                                     System.err.print(" " +strvalue);
                                     if(str.equalsIgnoreCase("url")){
                                    	 dataSource.setUrl(strvalue);
                                     }else if(str.equalsIgnoreCase("username")){
                                    	 dataSource.setUsername(strvalue); 
                                     }else if(str.equalsIgnoreCase("password")){
                                    	 dataSource.setPassword(strvalue);
                                     }
                                }
                             }
                            dataSource.setShutdown(false);
                            mysqlDBNode.getDataSourceList().add(dataSource);//添加多个dataSourceNodes信息
                         }    
                     } 
                 }
                 System.out.println("第"+i+"个datanode");
                 cluster.getClusterContext().getSourceDBNodeList().add(mysqlDBNode);
             }//添加多个datanode节点
           return  cluster.getClusterContext().getSourceDBNodeList();
		} catch (DocumentException e) {
			e.printStackTrace();
			return null;  
		}
		
      
        
    }
}